<?php
/**
 * Booksコントローラー
 */
class BooksController extends AppController
{
    var $name = 'Books';
    var $uses = array('Book');

    public function getAllBooks()
    {
        // 全件取得
        $books = $this->Book->find('all');
        
        // FlexのArrayCollectionに対応したクラスに変換･･･してるっぽい
        // (/amfphp/services/vo/ArrayCollection.php)
        $booksCollection = new ArrayCollection($books);

        return $booksCollection;
    }
    
    public function getBookById($id)
    {
        // idをキーにデータを取得
        $book = $this->Book->findById($id);
        
        return $book;
    }
    
    public function getBooksByConditions($bookName,$writerName,$publisher,$writeymdFrom,$writeymdTo ,$buyymdFrom,$buyymdTo,$priceFrom,$priceTo,$classification,$order,$isOnlyBuy)
    {

        // 検索条件の設定
        $conditions['conditions'] = array();
        if ($bookName != "") {
            //$conditions['conditions'] = array('Book.name LIKE' => $bookName  . '%');
            array_push($conditions['conditions'], array('Book.name LIKE' => $bookName  . '%'));
        }
        if ($writerName != "") {
            //$conditions['conditions'] = array('Book.writername LIKE' => $writerName . '%');
            array_push($conditions['conditions'], array('Book.writername LIKE' => $writerName . '%'));
        }
        if ($publisher != "") {
            //$conditions['conditions'] = array('Book.publisher' => $publisher);
            array_push($conditions['conditions'], array('Book.publisher' => $publisher));
        }
        if ($writeymdFrom != "" && $writeymdTo != "") {
            //$conditions['conditions'] = array('Book.writeymd BETWEEN ? AND ?' => array($writeymdFrom,$writeymdTo)) ;
            array_push($conditions['conditions'], array('Book.writeymd BETWEEN ? AND ?' => array($writeymdFrom,$writeymdTo)));
        }
        if ($buyymdFrom != "" && $buyymdTo != "") {
            //$conditions['conditions'] = array('Book.buyymd BETWEEN ? AND ?' => array($buyymdFrom,$buyymdTo)) ;
            array_push($conditions['conditions'], array('Book.buyymd BETWEEN ? AND ?' => array($buyymdFrom,$buyymdTo)));
        }
        if ($priceFrom != "" && $priceTo != "") {
            //$conditions['conditions'] = array('Book.price BETWEEN ? AND ?' => array($priceFrom,$priceTo)) ;
            array_push($conditions['conditions'], array('Book.price BETWEEN ? AND ?' => array($priceFrom,$priceTo)));
        }
        if ($classification != "") {
            //$conditions['conditions'] = array('Book.classification' => $classification);
            array_push($conditions['conditions'], array('Book.classification' => $classification));
        }
        if ($isOnlyBuy == "1") {
            //$conditions['conditions'] = array ("not" => array ("Book.buyymd" => null,));
            array_push($conditions['conditions'], array ("not" => array ("Book.buyymd" => null,)));
        }
        $condition = $conditions;

        //並び順の指定
        if ($order == "1") {
        	$condition['order'] = array('Book.name');
        } else if ($order == "2") {
        	$condition['order'] = array('Book.writername');
        } else if ($order == "3") {
        	$condition['order'] = array('Book.publisher');
        }
        
         $this->log($condition, LOG_DEBUG);
        
        // 全件取得
        $books = $this->Book->find('all',$conditions);
        
        $this->log($books, LOG_DEBUG);
        
        // FlexのArrayCollectionに対応したクラスに変換･･･してるっぽい
        // (/amfphp/services/vo/ArrayCollection.php)
        $booksCollection = new ArrayCollection($books);

        return $booksCollection;
    }
    
    // 保存アクション
    public function saveBook($book)
    {
        return $this->Book->save($book);
    }

    public function removeBookById($id)
    {
        // idをキーにデータを削除
        return $this->Book->delete($id);

    }

    public function test()
    {
        return "Hello World";
    }
}
